package com.ruoyi.hotSpot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hotSpot.domain.A2DynamicsInfo;
import com.ruoyi.hotSpot.domain.A2StaticInfo;
import com.ruoyi.hotSpot.domain.dto.A2StaticInfoCondition;
import com.ruoyi.hotSpot.mapper.A2StaticInfoMapper;
import com.ruoyi.hotSpot.service.IA2DynamicsInfoService;
import com.ruoyi.hotSpot.service.IA2StaticInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * AIS/ADS-B静态信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-14
 */
@Service
public class A2StaticInfoServiceImpl extends ServiceImpl<A2StaticInfoMapper, A2StaticInfo> implements IA2StaticInfoService
{
    @Autowired
    private A2StaticInfoMapper a2StaticInfoMapper;

    @Autowired
    private IA2DynamicsInfoService a2DynamicsInfoService;

    /**
     * 查询AIS/ADS-B静态信息
     *c
     * @param id AIS/ADS-B静态信息主键
     * @return AIS/ADS-B静态信息
     */
    @Override
    public A2StaticInfo selectA2StaticInfoById(Long id)
    {
        return a2StaticInfoMapper.selectA2StaticInfoById(id);
    }

    /**
     * 查询AIS/ADS-B静态信息列表
     *
     * @param a2StaticInfo AIS/ADS-B静态信息
     * @return AIS/ADS-B静态信息
     */
    @Override
    public List<A2StaticInfo> selectA2StaticInfoList(A2StaticInfo a2StaticInfo)
    {
        return a2StaticInfoMapper.selectA2StaticInfoList(a2StaticInfo);
    }

    /**
     * 新增AIS/ADS-B静态信息
     *
     * @param a2StaticInfo AIS/ADS-B静态信息
     * @return 结果
     */
    @Override
    public int insertA2StaticInfo(A2StaticInfo a2StaticInfo)
    {
        a2StaticInfo.setCreateTime(DateUtils.getNowDate());
        return a2StaticInfoMapper.insertA2StaticInfo(a2StaticInfo);
    }

    /**
     * 修改AIS/ADS-B静态信息
     *
     * @param a2StaticInfo AIS/ADS-B静态信息
     * @return 结果
     */
    @Override
    public int updateA2StaticInfo(A2StaticInfo a2StaticInfo)
    {
        a2StaticInfo.setUpdateTime(DateUtils.getNowDate());
        return a2StaticInfoMapper.updateA2StaticInfo(a2StaticInfo);
    }

    /**
     * 批量删除AIS/ADS-B静态信息
     *
     * @param ids 需要删除的AIS/ADS-B静态信息主键
     * @return 结果
     */
    @Override
    public int deleteA2StaticInfoByIds(Long[] ids)
    {
        return a2StaticInfoMapper.deleteA2StaticInfoByIds(ids);
    }

    /**
     * 删除AIS/ADS-B静态信息信息
     *
     * @param id AIS/ADS-B静态信息主键
     * @return 结果
     */
    @Override
    public int deleteA2StaticInfoById(Long id)
    {
        return a2StaticInfoMapper.deleteA2StaticInfoById(id);
    }

    @Override
    public TableDataInfo page(A2StaticInfoCondition condition) {
        QueryWrapper<A2StaticInfo> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(condition.getWords())){
            wrapper.like("name", condition.getWords());
        }
        if(condition.getTargetClass()!=null){
            wrapper.eq("type", condition.getTargetClass());
        }
        wrapper.orderByDesc("id");
        IPage<A2StaticInfo> page = page(new Page<>(condition.getPageNum(), condition.getPageSize()), wrapper);
        for(A2StaticInfo bean:page.getRecords()){
            //根据【mmsiIcao】查询最后一个动态点位
            A2DynamicsInfo last = a2DynamicsInfoService.getLast(bean.getMmsiIcao());
            if(last!=null){
                bean.setLon(last.getLon());
                bean.setLat(last.getLat());
            }
        }
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }
}
